package com.group9.handinhand.dao.model;

import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;

import org.apache.commons.lang.ArrayUtils;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import com.group9.handinhand.service.so.FriendshipSO;

@Entity
@Table(name = "FRIENDSHIP", uniqueConstraints = @UniqueConstraint(columnNames = {"FIRST_USER_ID", "SECOND_USER_ID" }))
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Friendship extends AbstractBaseModel {
	private static final long serialVersionUID = 1L;
	
	@ManyToOne
    @JoinColumn(name = "FIRST_USER_ID", nullable = false)
	private User firstUser;
	
	@ManyToOne
    @JoinColumn(name = "SECOND_USER_ID", nullable = false)
	private User secondUser;
	
	public FriendshipSO convertToServiceObject(String... ignoreProperties) {
		FriendshipSO so = (FriendshipSO) convert(new FriendshipSO(), 
			(String[]) ArrayUtils.addAll(ignoreProperties, new String[]{"firstUser","secondUser"}));
		so.setFirstUser(firstUser.convertToServiceObject(new String[]{"socialUsers"}));
		so.setSecondUser(secondUser.convertToServiceObject(new String[]{"socialUsers"}));
		return so;
	}

	public User getFirstUser() {
		return firstUser;
	}

	public void setFirstUser(User firstUser) {
		this.firstUser = firstUser;
	}

	public User getSecondUser() {
		return secondUser;
	}

	public void setSecondUser(User secondUser) {
		this.secondUser = secondUser;
	}

	@Override
	public String toString() {
		return "Friendship [firstUser=" + firstUser + ", secondUser="
				+ secondUser + "]";
	}
}
